package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.DApartmentMapper;
import org.example.mapper.DStaffMapper;
import org.example.pojo.DApartment;
import org.example.pojo.DApplyApartment;
import org.example.pojo.DStaff;
import org.example.pojo.select.DApplyApartmentSelect;
import org.example.service.DApplyApartmentService;
import org.example.mapper.DApplyApartmentMapper;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author chenhongkai
* @description 针对表【d_apply_apartment(申请宿舍)】的数据库操作Service实现
* @createDate 2024-05-22 08:46:39
*/
@Service
public class DApplyApartmentServiceImpl extends ServiceImpl<DApplyApartmentMapper, DApplyApartment>
    implements DApplyApartmentService{

    @Autowired
    private DApplyApartmentMapper dApplyApartmentMapper;

    @Autowired
    private DStaffMapper dStaffMapper;

    @Autowired
    private DApartmentMapper dApartmentMapper;

    @Override
    public Result<Map> SelectAllApplyApartment(DApplyApartmentSelect dApplyApartmentSelect) {

        Page<DApplyApartment> page = new Page<>(dApplyApartmentSelect.getCurrentPage(),dApplyApartmentSelect.getPageSize());
        IPage<Map> mapIPage = dApplyApartmentMapper.SelectAllApplyApartment(page,dApplyApartmentSelect);

        Map applyApartmentMap = new HashMap<>();
        applyApartmentMap.put("pageSize",mapIPage.getSize());
        applyApartmentMap.put("pageTotal",mapIPage.getTotal());
        applyApartmentMap.put("applyApartmentData",mapIPage.getRecords());

        return Result.ok(applyApartmentMap);
    }

    @Override
    public Result<DApplyApartment> updateStatusApplyApartment(DApplyApartment dApplyApartment) {
        //更新时间
        dApplyApartment.setUpdateDate(new Date());

        DStaff dStaff = new DStaff();
        //更新时间
        dStaff.setCheckInDate(new Date());
        dStaff.setStaffId(dApplyApartment.getStaffId());
        dStaff.setApartmentId(dApplyApartment.getApartmentId());

        int rowUpdateStaff,rowUpdateApplyApartment;

        //通过审批
        if(dApplyApartment.getStatus() == 1){

            Integer vacancy = dApartmentMapper.selectVacancyById(dApplyApartment.getApartmentId());
            if (vacancy <= 0){
                return Result.warning(501,"宿舍床位不足");
            }
            //增加原先宿舍的空余床位
            DStaff selectStaff = dStaffMapper.selectApartmentByStaffId(dApplyApartment.getStaffId());
            dApartmentMapper.AddApartmentVacancy(selectStaff.getApartmentId());

            //修改人员宿舍信息
            rowUpdateStaff = dStaffMapper.updateApartment(dStaff);
            //修改申请信息
            rowUpdateApplyApartment = dApplyApartmentMapper.updateApplyById(dApplyApartment);

            //减少所换宿舍的空余床位
            dApartmentMapper.DeleteApartmentVacancy(dApplyApartment.getApartmentId());
            //验证是否成功修改
            if(rowUpdateStaff == 0 || rowUpdateApplyApartment == 0){
                return Result.error("审批修改状态错误");
            }
            //更改时间
            DApartment apartment = new DApartment();
            apartment.setApartmentId(dApplyApartment.getApartmentId());
            apartment.setUpdateDate(new Date());
            dApartmentMapper.updateByApartmentId(apartment);
            apartment.setApartmentId(selectStaff.getApartmentId());
            dApartmentMapper.updateByApartmentId(apartment);
        }
        else{ //不通过审批
            rowUpdateApplyApartment = dApplyApartmentMapper.updateApplyById(dApplyApartment);
            //验证是否成功修改
            if(rowUpdateApplyApartment == 0){
                return Result.error("修改状态错误");
            }
        }

        return Result.ok(null);
    }

    @Override
    public Result<DApplyApartment> ConfirmApartment(DApplyApartment dApplyApartment) {
        //更新时间
        dApplyApartment.setUpdateDate(new Date());
        dApplyApartment.setStatus(3);
        int row = dApplyApartmentMapper.updateApplyById(dApplyApartment);
        if(row == 0){
            return Result.error("确认房间失败");
        }
        return Result.ok(null);
    }

    @Override
    public Result<DApplyApartmentSelect> AddApply(DApplyApartmentSelect dApplyApartmentSelect) {
//        System.out.println("dApplyApartmentSelect = " + dApplyApartmentSelect);

        DApplyApartment dApplyApartment = new DApplyApartment();
        //更新时间
        dApplyApartment.setUpdateDate(new Date());
        dApplyApartment.setApartmentId(dApplyApartmentSelect.getApartmentId());
        dApplyApartment.setApplyMessage(dApplyApartmentSelect.getApplyMessage());
        dApplyApartment.setApplyType(dApplyApartmentSelect.getApplyType());

        //判断宿舍申请是否提交宿舍房间
        if(dApplyApartment.getApartmentId() != null){
            dApplyApartment.setStatus(3);
            //判断宿舍床位是否充足
            Integer vacancy = dApartmentMapper.selectVacancyById(dApplyApartmentSelect.getApartmentId());
            if(dApplyApartmentSelect.getStaffIds().length > vacancy){
                return Result.warning(501,"宿舍床位不足");
            }
        }else{
            dApplyApartment.setStatus(0);
        }
        for(String staffId : dApplyApartmentSelect.getStaffIds()){
            dApplyApartment.setStaffId(staffId);
            int row = dApplyApartmentMapper.insert(dApplyApartment);
            if(row == 0) return Result.error("添加宿舍申请失败");
        }
        return Result.ok(null);

//        if(dApplyApartment.getApartmentId() != null){
//            dApplyApartment.setStatus(3);
//        }else{
//            dApplyApartment.setStatus(0);
//        }
//        int row = dApplyApartmentMapper.insert(dApplyApartment);
//        if(row == 0) return Result.error("添加宿舍申请失败");
//        return Result.ok(null);
    }
}




